version 14.1

cd "H:\Survey eksperiment sector bias 2018\Data fra yougov"
use affaldsdata_16082022.dta

*when it says original study it refers to Baekgaard and serritzlew's study that we are replicating.

*-----------------------------
*RECODING AND PREPARING VARIABLES.

*generating new placebo variable.
*recipe using values on the variables Q_P and Random_Placebo: 
*affald b (Q_P = 2) = row random 1 (Random_Placebo=1) =correct
*affald a Q_P = 1) = row random 2 (Random_Placebo=2) = correct
*affald b Q_P = 2) = row random 2 (Random_Placebo=2) = wrong
*affald a Q_P = 1) = row random 1 (Random_Placebo=1) = wrong

gen Placebo=.
replace Placebo=1 if ((Q_P==2 & Random_Placebo==1.)|(Q_P==1 & Random_Placebo==2.)) 
replace Placebo=0 if ((Q_P==2. & Random_Placebo==2.) |(Q_P==1 & Random_Placebo==1.))
label variable Placebo  "Placebo recoded into correct and wrong answers" 
label values Placebo answer 


*creating variables for when placebo A and B is correct respectively
gen Placebo_A_correct = .
replace Placebo_A_correct=1 if (Q_P==1 & Random_Placebo==2)
replace Placebo_A_correct=0 if ((Q_P==2 & Random_Placebo==1) | (Q_P==1 & Random_Placebo==1) | (Q_P==2 & Random_Placebo==2))

gen Placebo_B_correct = .
replace Placebo_B_correct = 1 if (Q_P==2 & Random_Placebo==1)
replace Placebo_B_correct = 0 if ((Q_P==1 & Random_Placebo==2) | (Q_P==1 & Random_Placebo==1) | (Q_P==2 & Random_Placebo==2))

*Recoding variables again for the index measuring public service provision attitudes so that we have it in stata dofile.
*+ making the index again to rescale from 0-100 as in the experiment we are replicating.
*reversing the scale for two of the items in the index so it matches the third.
*in original study they reverse the third instead, because their scale is from completely agree to disagree
*whereas our is from disagree to agree. 

recode Q2_1 (1=5 "Completely disagree") (2=4 "Partly disagree") (3=3 "Neither agree nor disagree") (4=2 "Partly agree") (5=1 "Completely agree"), gen(Private_better)
label variable Private_better "Private sector best"

recode Q2_2 (1=5 "Completely disagree") (2=4 "Partly disagree") (3=3 "Neither agree nor disagree") (4=2 "Partly agree") (5=1 "Completely agree"), gen(More_contracting_out)
label variable More_contracting_out "More contracting out"


*factor analysis to check for latent attitude toward service provision:
*first original variabels and then recoded
pca Q2_1 Q2_2 Q2_3, comp(2) covariance
pca Private_better More_contracting_out Q2_3, comp(2) covariance


*checking reliability of index:
alpha Private_better More_contracting_out Q2_3


*constructing index and rescaling from 1-5 to 0-100 so it corresponds to the original study we are replicating.
*to rescale, we substract 1 from each variable to reduce response categories from 1-5 to 1-4.
*then we divide by the number of variables and multiply by 25.
*guiding source: https://measuringu.com/converting-scales-to-100-points/ 
generate Public_service_preference=((Q2_3-1)+(More_contracting_out-1)+(Private_better-1))/3*25
label variable Public_service_preference "Pro public service provision"


*correlation between ideology and sector preferences:
pwcorr Q1 Public_service_preference, sig


*creating new dummy variables to compare treatment and placebo.
gen T1_P = . 
replace T1_P = 1 if T1_correct==1 
replace T1_P = 1 if T1_correct==0 
replace T1_P = 0 if Placebo==1
replace T1_P = 0 if Placebo==0 
label variable T1_P "Treatment 1 compared to Placebo"
label define treatment 1 "Treatment" 0 "Placebo"
label values  T1_P treatment

gen T2_P = .
replace T2_P= 1 if T2_correct==1 
replace T2_P= 1 if T2_correct==0 
replace T2_P= 0 if Placebo==1
replace T2_P= 0 if Placebo==0 
label variable T2_P "Treatment 2 compared to Placebo"
label values  T2_P treatment

gen T3_P = .
replace T3_P= 1 if T3_correct==1 
replace T3_P= 1 if T3_correct==0 
replace T3_P= 0 if Placebo==1
replace T3_P= 0 if Placebo==0 
label variable T3_P "Treatment 3 compared to Placebo"
label values  T3_P treatment

gen T4_P = .
replace T4_P= 1 if T4_correct==1 
replace T4_P= 1 if T4_correct==0 
replace T4_P= 0 if Placebo==1
replace T4_P= 0 if Placebo==0 
label variable T4_P "Treatment 4 compared to Placebo"
label values  T4_P treatment

gen T5_P = .
replace T5_P= 1 if T5_correct==1 
replace T5_P= 1 if T5_correct==0 
replace T5_P= 0 if Placebo==1
replace T5_P= 0 if Placebo==0 
label variable T5_P "Treatment 5 compared to Placebo"
label values  T5_P treatment

gen T6_P = .
replace T6_P= 1 if T6_correct==1 
replace T6_P= 1 if T6_correct==0 
replace T6_P= 0 if Placebo==1
replace T6_P= 0 if Placebo==0 
label variable T6_P "Treatment 6 compared to Placebo"
label values  T6_P treatment

******* DEP_VAR_Treatment and Placebo - correct answer************ 
*coding new dependent variables to include placebo respondents.
*coding them so that all correct answers on both T1 and Placebo are '1' on 
*new variable and wrong answers on both are '0'.

gen DepT1_P = .
replace DepT1_P = 0 if T1_correct == 0 | Placebo == 0
replace DepT1_P = 1 if T1_correct == 1 | Placebo == 1
label variable DepT1_P "Public best"
label define answer 1 "Correct" 0 "Wrong"
label values DepT1_P answer
tab DepT1_P

gen DepT2_P = .
replace DepT2_P = 0 if T2_correct == 0
replace DepT2_P = 1 if T2_correct == 1
replace DepT2_P = 0 if Placebo == 0
replace DepT2_P = 1 if Placebo == 1
label variable DepT2_P "For-profit best"
label values DepT2_P answer

gen DepT3_P = .
replace DepT3_P = 0 if T3_correct == 0
replace DepT3_P = 1 if T3_correct == 1
replace DepT3_P = 0 if Placebo == 0
replace DepT3_P = 1 if Placebo == 1
label variable DepT3_P "Public best"
label values DepT3_P answer

gen DepT4_P = .
replace DepT4_P = 0 if T4_correct == 0
replace DepT4_P = 1 if T4_correct == 1
replace DepT4_P = 0 if Placebo == 0
replace DepT4_P = 1 if Placebo == 1
label variable DepT4_P "Nonprofit best"
label values DepT4_P answer

gen DepT5_P = .
replace DepT5_P = 0 if T5_correct == 0
replace DepT5_P = 1 if T5_correct == 1
replace DepT5_P = 0 if Placebo == 0
replace DepT5_P = 1 if Placebo == 1
label variable DepT5_P "For-profit best"
label values DepT5_P answer

gen DepT6_P = .
replace DepT6_P = 0 if T6_correct == 0
replace DepT6_P = 1 if T6_correct == 1
replace DepT6_P = 0 if Placebo == 0
replace DepT6_P = 1 if Placebo == 1
label variable DepT6_P "Nonprofit best"
label values DepT6_P answer


*---------------------------------------------
*generating one dependent variable for the full model.
generate treatment = .
replace treatment = 1 if DepT1_P==1 | DepT2_P==1 | DepT3_P==1 | DepT4_P==1 | DepT5_P==1 | DepT6_P == 1
replace treatment = 0 if DepT1_P==0 | DepT2_P==0 | DepT3_P==0 | DepT4_P==0 | DepT5_P==0 | DepT6_P==0
label values treatment Treatment

*recoding scenario to specify value labels.
recode Scenario (1=1 "Placebo")(2=2 "T1: Public best compared to for-profit") (3=3 "T2: For-profit best compared to public") (4=4 "T3: Public best compared to nonprofit") (5=5 "T4: Nonprofit best compared to public")(6=6 "T5: For-profit best compared to nonprofit")(7=7 "T6:Nonprofit best compared to for-profit"),gen (Scenario_new)

label variable Q1 "Political orientation (1=left;11=right)"

*-----------------------------------------------
*running full model.

logit treatment c.Public_service_preference##i.Scenario_new [pweight=weight], vce(robust)

eststo clear
logit treatment c.Public_service_preference##i.Scenario_new [pweight=weight], vce(robust)
eststo 
esttab using ExperimentAffald.rtf, replace wide varwidth(40) coeflabels(*) star(* .05 ** .01 *** .001) b(3) nonumbers label interaction(" * ") noconstant nobaselevels se(3) pr2(2) title(Experiment 1: Logistic Regression Analyses of Correct Choice of Best-Performing Refuse Collector) mtitle ("Model 1") addnotes (Note: Entries are logistic regression coefficients; robust standard errors in parentheses; OLS regression produced similar results.) 
eststo clear

quietly margins, at(Public_service_preference= (0 (10) 100)) by(T1_P)
marginsplot, recast(line) recastci(rarea) scheme(s1mono) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) xtitle("Public Sector Preference", height(+4)) ytitle("Correct Answer", height(+4)) title("T1 (Public Best Compared to For-Profit)", nospan) legend(order())
graph save graph3.gph, replace

quietly margins, at(Public_service_preference= (0 (10) 100)) by(T2_P)
marginsplot, recast(line) recastci(rarea) scheme(s1mono) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) xtitle("Public Sector Preference", height(+4)) ytitle("Correct Answer", height(+4)) title("T2 (For-Profit Best Compared to Public)", nospan)
graph save graph3a.gph, replace 

quietly margins, at(Public_service_preference= (0 (10) 100)) by(T3_P)
marginsplot, recast(line) recastci(rarea) scheme(s1mono) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) xtitle("Public Sector Preference", height(+4)) ytitle("Correct Answer", height(+4)) title("T3 (Public Best Compared to Nonprofit)", nospan)
graph save graph3b.gph, replace

quietly margins, at(Public_service_preference= (0 (10) 100)) by(T4_P)
marginsplot, recast(line) recastci(rarea) scheme(s1mono) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) xtitle("Public Sector Preference", height(+4)) ytitle("Correct Answer", height(+4)) title("T4 (Nonprofit Best Compared to Public)", nospan)
graph save graph3c.gph, replace
 
quietly margins, at(Public_service_preference= (0 (10) 100)) by(T5_P)
marginsplot, recast(line) recastci(rarea) scheme(s1mono) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) xtitle("Public Sector Preference", height(+4)) ytitle("Correct Answer", height(+4)) title("T5 (For-profit Best Compared to Nonprofit)", nospan)
graph save graph3d.gph, replace 

quietly margins, at(Public_service_preference= (0 (10) 100)) by(T6_P)
marginsplot, recast(line) recastci(rarea) scheme(s1mono) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) xtitle("Public Sector Preference", height(+4)) ytitle("Correct Answer", height(+4)) title("T6 (Nonprofit Best Compared to For-Profit)", nospan) 
graph save graph3e.gph, replace 

graph combine graph3.gph graph3a.gph graph3b.gph graph3c.gph graph3d.gph graph3e.gph, graphregion(color(white)) rows(3) xsize(8.5) ysize(8) iscale(.5)
graph export "allgraphsaffald.png", replace

*checking robustness with control variables and OLS
logit treatment c.Public_service_preference##i.Scenario_new [pweight=weight], vce(robust)

logit treatment c.Public_service_preference##i.Scenario_new  Q1 Male_dummy  age_18_34 age_35_49 age_50_64 [pweight=weight], vce(robust)

regress treatment c.Public_service_preference##i.Scenario_new [pweight=weight], vce(robust)


*---------------------------------------------------

*descriptive statistics for word
eststo clear
estpost summarize Public_service_preference T1_correct T2_correct T3_correct T4_correct T5_correct T6_correct Placebo Q1 Male_dummy age_18_34 age_35_49 age_50_64
esttab using DescriptivesE1.rtf, cells("count mean sd min max") title("Table 1. Summary Statistics for Refuse Collection (Experiment 1)") nonumber noobs label replace

*pairwise correlation using asdoc package to export to word (cite Attaullah Shah 2018).
*In-text citation: Tables were created using asdoc, a Stata program written by Shah (2018).
*Bibliography: Shah, A. (2018). ASDOC: Stata module to create high-quality tables in MS Word from Stata output. Statistical Software Components S458466, Boston College Department of Economics.
asdoc pwcorr Public_service_preference T1_correct T2_correct  T3_correct T4_correct T5_correct T6_correct Placebo, star(all) replace nonum

*descriptive tables for correct answers. 
asdoc tab1 T1_correct T2_correct T3_correct T4_correct T5_correct T6_correct Placebo, replace

*crosstable of public service preference and treatments to examine the percentage of correct and wrong answers
*among the most pro-public and most pro-private.
asdoc tabulate Public_service_preference T1_correct, nokey row replace

